SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[DnnProduct_CDS_TemplatesUpdate]
	@ID int,
	@DistributorID varchar(50),
	@Name nvarchar(255),
	@Description ntext,
	@Default bit,
	@Type varchar(20)
AS
BEGIN
	-- Change the status of all the assets using this template to 'Packaging Required'
	UPDATE [DnnProduct_CDS_Assets]
	SET PackageStatus = 0
	WHERE TemplateID = @ID

	IF (@Default = 1)
	BEGIN
		-- Remove Default flag from the rest of templates of the distributor
		UPDATE [DnnProduct_CDS_Templates]
		SET [Default] = 0
		WHERE DistributorID = @DistributorID AND [Type] = @Type
	END

	UPDATE [DnnProduct_CDS_Templates]
	SET
		[Name] = @Name,
		Description = @Description,
		[Type] = @Type,
		[Default] = @Default
	WHERE [ID] = @ID
END
GO
